//
//  YYSSearchController.m
//  FanLaiWu
//
//  Created by gonglei on 17/3/20.
//  Copyright © 2017年 yys. All rights reserved.
//

#import "YYSSearchController.h"
#import "YYSSeachResultController.h"
#import "YYSSearchHistroyCell.h"
@interface YYSSearchController () <UITextFieldDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *seacrText;
@property (weak, nonatomic) IBOutlet UIView *topcontentView;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation YYSSearchController
static NSString *ID = @"YYSSearchHistroyCell";
- (IBAction)clickquxiao:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)clickrecond:(UIButton *)sender {
    [[SaveManager shareSaveManager] cleanHistory];
    [self.collectionView reloadData];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self pressButton:textField.text];
    return YES;
}

- (void)pressButton:(NSString *)str {
    if([_seacrText isFirstResponder]){
        [_seacrText resignFirstResponder];
    }
    if (![str isEqualToString:@""]) {
        [[SaveManager shareSaveManager] saveSeachHistroy:str];
        YYSSeachResultController *deail = [[YYSSeachResultController alloc] init];
//        deail.title = @"搜索结果";
        [self.navigationController pushViewController:deail animated:YES];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
  
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.topView.layer.masksToBounds = YES;
    self.topView.layer.cornerRadius = 10;
    self.topcontentView.layer.masksToBounds = YES;
    self.topcontentView.layer.cornerRadius = 10;
    self.seacrText.delegate = self;
    self.seacrText.returnKeyType = UIReturnKeySearch;
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    
    
    layout.itemSize = CGSizeMake(([UIScreen mainScreen].bounds.size.width)/2  , 30);
    
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collectionView.collectionViewLayout = layout;
    //    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:242/255.0 green:242/255.0 blue:242/255.0 alpha:1];
    // 注册
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([YYSSearchHistroyCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
}
#pragma mark - <UICollectionViewDelegate,UICollectionViewDataSource>
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [[SaveManager shareSaveManager] getSeachHistroy].count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YYSSearchHistroyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.str = [[SaveManager shareSaveManager] getSeachHistroy][indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    YYSSeachResultController *deail = [[YYSSeachResultController alloc] init];
//    deail.title = @"搜索结果";
    [self.navigationController pushViewController:deail animated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [self.collectionView reloadData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
